//
//  ShareTimeEditVC.m
//  QRMaster
//
//  Created by lc on 16/6/13.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "ShareTimeEditVC.h"
#import "shareDimensionalView.h"
#import "TouchBtnColor.h"
#import "PromptInfoView.h"
#import "RequestNetWork.h"
#import "Reachability.h"
#import "StartPageController.h"
#import "validateInputValue.h"
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import "AreaChooseController.h"
#import "IQKeyboardManager.h"
#import "InternationalControl.h"
#import "shareDimensionalView.h"
#import <ShareSDK/ShareSDK.h>
#import <AudioToolbox/AudioToolbox.h>
#import "ShareUIView.h"
#import <MessageUI/MessageUI.h>
#import "UsingGuideController.h"
#import "shareDimensionalView.h"
@interface ShareTimeEditVC ()<shareDimensionaViewDelegate,UITextFieldDelegate,ABPeoplePickerNavigationControllerDelegate,UINavigationControllerDelegate,MFMessageComposeViewControllerDelegate,ShareViewDelegate,ABPeoplePickerNavigationControllerDelegate>
{
    UIView * mobileBgView;
    UIButton * areaBtn;
    UITextField * mobilTextField;
    UIButton * addAddressTelePhoneBtn;
    
    UIButton * startBtn;
    UIButton * endBtn;
    UILabel * startLabel;
    UILabel * endLabel;
    UILabel * startTimeLabel;
    UILabel * endTimeLabel;
    
    NSString * beginTime;
    NSString * endTime;
    
    UIButton * shareBtn;
    UILabel * xieyiLabel;
    
//    UIWindow * timeKeyWindow;
    UIView * backGroundView;
    UIView * timeSelectView;
    UIDatePicker * selectPickView;
    NSString * dateString;
    NSString * timeString;
    UIButton * dateBtn;
    UIButton * timeBtn;
    NSDate * lastDate;//最多可以分享的时间
    NSMutableArray * postArray;//上传信息参数
    NSInteger secs;//算出九十天后的日期
    NSInteger shareSecs;//算出三十天后的日期
    NSString * localString;
    UIButton * makeSureBtn;
    
    id<ISSContent> publishContent; //分享内容
    NSData * imgData;
    NSData * shareLinkImgData;
    ShareUIView * shareView;  //分享view
    NSString * linkURLNO;

    shareDimensionalView * sdv;
    int currentPattern; // 1:开始 0：结束
    //多语言
//    UILabel * titleLabel;
}


@end

@implementation ShareTimeEditVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColorFromRGB(0xebebeb);
    self.navigationItem.title = @"分享房卡";
    
    beginTime = nil;
    endTime = nil;
    secs = 90 * 24 * 3600;
    shareSecs = 30 * 24 * 3600;
    if ([[InternationalControl userLanguage]isEqualToString:@"en"]) {
        localString = @"en_US";
    }
    else if ([[InternationalControl userLanguage]isEqualToString:@"zh-Hans"])
    {
        localString = @"zh_CN";
    }
    else
    {
        localString = @"zh-Hant";
    }
    [self prepareGroundBackView];
    [self prepareTimeSelectView];
    [self prepareEditView];
    [self prepareShareView];
}

- (void)prepareEditView{
    //手机号区域
    mobileBgView = [[UIView  alloc] initWithFrame:CGRectMake(0, 16, UI_SCREEN_WIDTH, 44)];
    mobileBgView.backgroundColor = [UIColor whiteColor];
    areaBtn = [[UIButton alloc] initWithFrame:CGRectMake(16, 14, 40, 16)];
    [areaBtn setTitle:@"+86" forState:UIControlStateNormal];
    [areaBtn setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
    areaBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    areaBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [areaBtn addTarget:self action:@selector(chooseArea) forControlEvents:UIControlEventTouchUpInside];
    [mobileBgView addSubview:areaBtn];
    UIView * sepLine = [[UIView alloc] initWithFrame:CGRectMake(72, 0, 0.5, 44)];
    sepLine.backgroundColor = UIColorFromRGB(0xc1c1c1);
    [mobileBgView addSubview:sepLine];
    mobilTextField = [[UITextField alloc] initWithFrame:CGRectMake(88, 0, UI_SCREEN_WIDTH-88-45, 44)];
    mobilTextField.textColor = UIColorFromRGB(0x333333);
    mobilTextField.font = [UIFont systemFontOfSize:15];
    mobilTextField.tag = 101;
    mobilTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"添加联系人（手机号）" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:UIColorFromRGB(0xc1c1c1)}];
    [mobilTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [mobileBgView addSubview:mobilTextField];
    addAddressTelePhoneBtn = [[UIButton alloc] initWithFrame:CGRectMake(UI_SCREEN_WIDTH-40, 10, 24, 24)];
    [addAddressTelePhoneBtn setBackgroundImage:[UIImage imageNamed:@"ic-txl-green"] forState:UIControlStateNormal];
    [addAddressTelePhoneBtn addTarget:self action:@selector(chooseLinkman) forControlEvents:UIControlEventTouchUpInside];
    [mobileBgView addSubview:addAddressTelePhoneBtn];
    UIView * upLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 0.5)];
    upLine.backgroundColor = UIColorFromRGB(0xc1c1c1);
    UIView * downLine = [[UIView alloc] initWithFrame:CGRectMake(0, 43.5, UI_SCREEN_WIDTH, 0.5)];
    downLine.backgroundColor = UIColorFromRGB(0xc1c1c1);
    [mobileBgView addSubview:upLine];
    [mobileBgView addSubview:downLine];
    [self.view addSubview:mobileBgView];
    
    //编辑时间区域
    //开始
    startBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 76, UI_SCREEN_WIDTH, 44)];
    startBtn.backgroundColor = [UIColor whiteColor];
    startBtn.tag = 101;
    [startBtn addTarget:self action:@selector(startTimeSelect:) forControlEvents:UIControlEventTouchUpInside];
    startLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 14, 60, 16)];
    startLabel.textAlignment = NSTextAlignmentLeft;
    startLabel.text = @"起始时间";
    startLabel.font = [UIFont systemFontOfSize:15];
    startLabel.textColor = UIColorFromRGB(0x333333);
    [startBtn addSubview:startLabel];
    startTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(UI_SCREEN_WIDTH-150-16, 14, 150, 16)];
    startTimeLabel.textColor = UIColorFromRGB(0x7dc234);
    startTimeLabel.font = [UIFont systemFontOfSize:15];
    startTimeLabel.textAlignment = NSTextAlignmentRight;
    [startBtn addSubview:startTimeLabel];
    UIView * Upline = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 0.5)];
    Upline.backgroundColor = UIColorFromRGB(0xc1c1c1);
    [startBtn addSubview:Upline];
    UIView * midline = [[UIView alloc] initWithFrame:CGRectMake(16, 43.5, UI_SCREEN_WIDTH, 0.5)];
    midline.backgroundColor = UIColorFromRGB(0xc1c1c1);
    [startBtn addSubview:midline];
    [self.view addSubview:startBtn];
    
    //结束
    endBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 120, UI_SCREEN_WIDTH, 43.5)];
    endBtn.backgroundColor = [UIColor whiteColor];
    endBtn.tag = 100;
    [endBtn addTarget:self action:@selector(endTimeSelect:) forControlEvents:UIControlEventTouchUpInside];
    endLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 14, 60, 16)];
    endLabel.textAlignment = NSTextAlignmentLeft;
    endLabel.text = @"结束时间";
    endLabel.font = [UIFont systemFontOfSize:15];
    endLabel.textColor = UIColorFromRGB(0x333333);
    [endBtn addSubview:endLabel];
    endTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(UI_SCREEN_WIDTH-150-16, 14, 150, 16)];
    endTimeLabel.textColor = UIColorFromRGB(0x7dc234);
    endTimeLabel.font = [UIFont systemFontOfSize:15];
    endTimeLabel.textAlignment = NSTextAlignmentRight;
    [endBtn addSubview:endTimeLabel];
    UIView * downline = [[UIView alloc] initWithFrame:CGRectMake(0, 43, UI_SCREEN_WIDTH, 0.5)];
    downline.backgroundColor = UIColorFromRGB(0xc1c1c1);
    [endBtn addSubview:downline];
    [self.view addSubview:endBtn];
    
    //分享按钮
    shareBtn = [[UIButton alloc] initWithFrame:CGRectMake(16, endBtn.frame.origin.y+43.5+26, UI_SCREEN_WIDTH-32, 48)];
    shareBtn.layer.cornerRadius = 5.0;
    shareBtn.layer.masksToBounds = YES;
    [shareBtn setTitle:@"分享" forState:UIControlStateNormal];
    [shareBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    shareBtn.titleLabel.font = [UIFont systemFontOfSize:19];
    shareBtn.backgroundColor = UIColorFromRGB(0xc1c1c1);
    shareBtn.enabled = NO;
    [shareBtn addTarget:self action:@selector(shareCard) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:shareBtn];
    
    //分享协议
    xieyiLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, shareBtn.frame.origin.y+48+20, 280, 13)];
    xieyiLabel.text = @"点击分享，即表示您已同意《分享协议》。";
    xieyiLabel.font = [UIFont systemFontOfSize:11];
    xieyiLabel.textColor = UIColorFromRGB(0x7dc234);
    [xieyiLabel sizeToFit];
    xieyiLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer * xieyiTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shareXieyi)];
    [xieyiLabel addGestureRecognizer:xieyiTap];
    [self.view addSubview:xieyiLabel];
    
}

-(void)prepareGroundBackView
{
    float ScreenHeight = UI_SCREEN_HEIGHT;
    float ScreenWidth = UI_SCREEN_WIDTH;
    backGroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    backGroundView.backgroundColor = [UIColor blackColor];
    backGroundView.alpha = 0.6;
    //    [self.view addSubview:backGroundView];
    [[UIApplication sharedApplication].keyWindow addSubview:backGroundView];
    backGroundView.hidden = YES;
    UITapGestureRecognizer * cancelTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelView)];
    [backGroundView addGestureRecognizer:cancelTap];
}

- (void)prepareTimeSelectView{
    
    timeSelectView = [[UIView alloc]initWithFrame:CGRectMake(10, (UI_SCREEN_HEIGHT-256)/2, UI_SCREEN_WIDTH-2*10, 256)];
//    timeSelectView.layer.borderColor = RGBA(125, 194, 52, 1).CGColor;
    timeSelectView.backgroundColor = RGBA(237, 238, 241, 1);
    timeSelectView.layer.borderWidth = 1.0;
    timeSelectView.layer.cornerRadius = 5;
    timeSelectView.layer.masksToBounds = YES;
    timeSelectView.hidden = YES;
    
    timeSelectView.userInteractionEnabled = YES;
    timeSelectView.layer.shadowColor = [UIColor blackColor].CGColor;
    timeSelectView.layer.shadowOffset = CGSizeMake(0, 3);
    timeSelectView.layer.shadowRadius = 3.0;
    timeSelectView.layer.shadowOpacity = 0.8;
    [[UIApplication sharedApplication].keyWindow addSubview:timeSelectView];
    
    dateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    dateBtn.frame = CGRectMake(0, 0, timeSelectView.frame.size.width/2, 30);
    [dateBtn setTitleColor:[UIColor blackColor] forState:0];
    [dateBtn setBackgroundColor:[UIColor colorWithRed:125/255.0 green:194/255.0 blue:52/255.0 alpha:0.18]];
    [dateBtn addTarget:self action:@selector(selectDate:) forControlEvents:UIControlEventTouchUpInside];
    [timeSelectView addSubview:dateBtn];
    
    UIView * dateLineView = [[UIView alloc]initWithFrame:CGRectMake(timeSelectView.frame.size.width/2, 0, 1, 30)];
    dateLineView.backgroundColor = RGBA(125, 194, 52, 1);
    [timeSelectView addSubview:dateLineView];
    
    timeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    timeBtn.frame = CGRectMake(timeSelectView.frame.size.width/2-1, 0, timeSelectView.frame.size.width/2, 30);
    [timeBtn setTitleColor:[UIColor blackColor] forState:0];
    [timeBtn addTarget:self action:@selector(selectTime:) forControlEvents:UIControlEventTouchUpInside];
    [timeSelectView addSubview:timeBtn];
    
    UIView * timeLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 30, timeSelectView.frame.size.width, 1)];
    timeLineView.backgroundColor = RGBA(125, 194, 52, 1);
    [timeSelectView addSubview:timeLineView];
    

    selectPickView = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 25, timeSelectView.frame.size.width, 196)];
    selectPickView.datePickerMode = UIDatePickerModeDate;
    [selectPickView addTarget:self action:@selector(selectDateTime:) forControlEvents:UIControlEventValueChanged];
    [timeSelectView addSubview:selectPickView];

    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0.0];
    NSDate * endDate = [NSDate dateWithTimeIntervalSinceNow:secs];
    // 打印日期：中间的空格可以用'at'或'T'等字符来分隔
    NSDateFormatter *dateFomatter = [[NSDateFormatter alloc] init];
    [dateFomatter setDateFormat:@"yyyy-MM-dd"];
    [dateFomatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:localString]];
    [dateFomatter stringFromDate:date];
    [dateFomatter stringFromDate:endDate];
    dateString = [dateFomatter stringFromDate:date];
    [dateBtn setTitle:dateString forState:0];
    selectPickView.minimumDate = date;
    selectPickView.maximumDate = endDate;
    lastDate = endDate;
    
    [dateFomatter setDateFormat:@"HH:mm"];
    timeString = [dateFomatter stringFromDate:date];
    [timeBtn setTitle:timeString forState:0];

    makeSureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    makeSureBtn.frame = CGRectMake(0, selectPickView.frame.origin.y+196-5, timeSelectView.frame.size.width, 40);
    [makeSureBtn setTitle:NSLocalizedString(@"alterSure", nil) forState:0];
    [makeSureBtn setTitleColor:RGBA(135, 199, 66, 1) forState:0];
    makeSureBtn.layer.cornerRadius = 5;
    makeSureBtn.layer.masksToBounds = YES;
    makeSureBtn.layer.borderWidth = 1.0;
    makeSureBtn.layer.borderColor = RGBA(125, 194, 52, 1).CGColor;
    [makeSureBtn addTarget:self action:@selector(makeSure:) forControlEvents:UIControlEventTouchUpInside];
    [timeSelectView addSubview:makeSureBtn];
}



- (void)prepareShareView{
    shareView = [[ShareUIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    shareView.delegate = self;
    shareView.hidden = YES;
    [[UIApplication sharedApplication].keyWindow addSubview:shareView];
}

#pragma mark - 按钮点击事件
- (void)startTimeSelect:(UIButton *)button{
    [mobilTextField resignFirstResponder];
    currentPattern = button.tag-100;
    timeSelectView.hidden = NO;
    backGroundView.hidden = NO;
}

- (void)endTimeSelect:(UIButton *)button{
    [mobilTextField resignFirstResponder];
    currentPattern = button.tag-100;
    timeSelectView.hidden = NO;
    backGroundView.hidden = NO;
}

- (void)shareCard{
    
    if (endTimeLabel.text == nil || [endTimeLabel.text isEqualToString:@""]) {
        [PromptInfoView showMessage:NSLocalizedString(@"endTimeNil", nil) superView:self.view];
        return;
    }
    if (startTimeLabel.text == nil || [startTimeLabel.text isEqualToString:@""]) {
        [PromptInfoView showMessage:NSLocalizedString(@"beginTimeNil", nil) superView:self.view];
        return;
    }
    if (![validateInputValue isPureNumandCharacters:mobilTextField.text]) {
        [PromptInfoView showMessage:NSLocalizedString(@"AccountError", nil) superView:self.view];
        return;
    }
    if (mobilTextField.text == nil || [mobilTextField.text isEqualToString:@""]) {
        [PromptInfoView showMessage:NSLocalizedString(@"AccountError", nil) superView:self.view];
        return;
    }
    if ([self IsHaveNetwork]) {
        return;
    }
    [self startWaiting:nil color:nil addToView:self.view];
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    [postDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [postDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [postDict setValue:_communityId forKey:@"communityId"];
    [postDict setValue:_roomId forKey:@"roomId"];
    [postDict setValue:endTimeLabel.text forKey:@"endTime"];
    [postDict setValue:startTimeLabel.text forKey:@"startTime"];
    [postDict setValue:mobilTextField.text forKey:@"mobile"];
    [postDict setValue:[areaBtn.titleLabel.text substringFromIndex:1]forKey:@"areaCode"];
    __weak ShareTimeEditVC * weakSelf = self;
    RequestNetWork * rnk = [[RequestNetWork alloc]init];
    [rnk initWithPath:POST(ADDCLIENTSHARE) params:postDict block:^(id downLoadData, id error) {
        if (downLoadData != nil)
        {
            id data = [downLoadData objectForKey:@"data"];
            linkURLNO = data;
            if (data) {
                [self shareType];
                
            }
        }
        else if (error != nil)
        {
            DDLOG(@"分享error = %@",error);
            [PromptInfoView showMessage:[error objectForKey:@"info"]];
            
        }
        [weakSelf stopWaiting];
    }];

}

-(void)shareType
{
    //构造分享内容
    publishContent = [ShareSDK content:NSLocalizedString(@"shareContent", nil)
                        defaultContent:NSLocalizedString(@"shareDefualt", nil)
                                 image:[ShareSDK imageWithData:imgData fileName:@"image" mimeType:@"jpg"]
                                 title:NSLocalizedString(@"shareTitle", nil)
                                   url:nil
                           description:nil
                             mediaType:SSPublishContentMediaTypeImage];
    [publishContent addQQUnitWithType:[NSNumber numberWithInt:SSPublishContentMediaTypeNews] content:NSLocalizedString(@"shareContent", nil) title:NSLocalizedString(@"shareTitle", nil) url:[NSString stringWithFormat:@"%@",linkURLNO] image:[ShareSDK imageWithData:shareLinkImgData fileName:@"image" mimeType:@"jpg"]];
    [publishContent addWeixinSessionUnitWithType:[NSNumber numberWithInt:SSPublishContentMediaTypeNews] content:NSLocalizedString(@"shareContent", nil) title:NSLocalizedString(@"shareTitle", nil) url:[NSString stringWithFormat:@"%@",linkURLNO] image:[ShareSDK imageWithData:shareLinkImgData fileName:@"image" mimeType:@"jpg"] musicFileUrl:nil extInfo:nil fileData:nil emoticonData:nil];
    
    shareView.hidden = NO;
    
}

#pragma mark ----- 分享点击图片事件
-(void)shareClick:(NSInteger)shareIndex
{
    [self cancelShare];
    int shareType = 0;
    if (shareIndex == 2) {
        [self sendSMS];
        return;
    }
    else if (shareIndex == 0) {
        shareType = ShareTypeQQ;
    }
    else if (shareIndex == 1)
    {
        shareType = ShareTypeWeixiSession;
    }
    
    [ShareSDK showShareViewWithType:shareType container:nil content:publishContent statusBarTips:YES authOptions:nil shareOptions:nil result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
        DDLOG(@"分享是否成功=%d",state);
        if (state == SSResponseStateFail)
        {
            [PromptInfoView showMessage:NSLocalizedString(@"shareFail", nil)] ;
        }
        [self.navigationController popViewControllerAnimated:YES];
        
    }];
}

#pragma mark ------取消分享
-(void)cancelShare
{
    shareView.hidden = YES;
}

#pragma mark --------  发送短信
-(void)sendSMS
{
    if ([MFMessageComposeViewController canSendText]) {
        MFMessageComposeViewController * messageController = [[MFMessageComposeViewController alloc]init];
        messageController.delegate = self;
        messageController.messageComposeDelegate = self;
        NSString * messageContent = [NSString stringWithFormat:@"%@%@%@",NSLocalizedString(@"messageFirst", nil), linkURLNO,NSLocalizedString(@"messageSecond", nil)];
        messageController.body = messageContent;
        messageController.recipients = @[mobilTextField.text];
        [self presentViewController:messageController animated:YES completion:^{
            [self cancelShare];
        }];
    }
    else
    {
        [PromptInfoView showMessage:NSLocalizedString(@"shareFail", nil)] ;
    }
    
}

-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


-(void)textFieldDidChange:(UITextField *)textField{
        [self judgeShareBtnStatus];
}

#pragma mark - 时间选择器点击事件
-(void)selectDateTime:(UIDatePicker *)pickView
{
    NSDateFormatter * df = [NSDateFormatter new];
    [df setDateFormat:@"yyyy-MM-dd"];
    [df setLocale:[[NSLocale alloc]initWithLocaleIdentifier:localString]];
    if (pickView.datePickerMode == UIDatePickerModeDate) {
        dateString = [df stringFromDate:pickView.date];
        [dateBtn setTitle:dateString forState:0];
    }
    else
    {
        [df setDateFormat:@"HH:mm"];
        timeString = [df stringFromDate:pickView.date];
        [timeBtn setTitle:timeString forState:0];
    }
}

-(void)selectDate:(UIButton *)btn
{
    [timeBtn setBackgroundColor:[UIColor clearColor]];
    [dateBtn setBackgroundColor:[UIColor colorWithRed:125/255.0 green:194/255.0 blue:52/255.0 alpha:0.18]];
    selectPickView.datePickerMode = UIDatePickerModeDate;
    if (!currentPattern) {
        selectPickView.datePickerMode = UIDatePickerModeDate;
        NSDateFormatter * df = [NSDateFormatter new];
        [df setDateFormat:@"yyyy-MM-dd"];
        [df setLocale:[[NSLocale alloc]initWithLocaleIdentifier:localString]];
        selectPickView.minimumDate = [df dateFromString:dateString];
        [timeBtn setTitle:timeString forState:0];
    }
    else
    {
        NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0.0];
        // 打印日期：中间的空格可以用'at'或'T'等字符来分隔
        NSDateFormatter *dateFomatter = [[NSDateFormatter alloc] init];
        [dateFomatter setDateFormat:@"yyyy-MM-dd"];
        [dateFomatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:localString]];
        [dateFomatter stringFromDate:date];
        selectPickView.minimumDate = date;
        [timeBtn setTitle:timeString forState:0];
    }
    
    
}

-(void)selectTime:(UIButton *)btn
{
    [dateBtn setBackgroundColor:[UIColor clearColor]];
    [timeBtn setBackgroundColor:[UIColor colorWithRed:125/255.0 green:194/255.0 blue:52/255.0 alpha:0.18]];
    selectPickView.datePickerMode = UIDatePickerModeCountDownTimer;
    if (!currentPattern) {
        NSDateFormatter *dateFomatter = [[NSDateFormatter alloc] init];
        [dateFomatter setDateFormat:@"HH:mm"];
        [dateFomatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:localString]];
        selectPickView.minimumDate =[dateFomatter dateFromString:timeString];
    }
    [dateBtn setTitle:dateString forState:0];
}

-(void)makeSure:(UIButton *)btn
{
    if (currentPattern) {
        startTimeLabel.text = [NSString stringWithFormat:@"%@ %@",dateString,timeString];
//        if(_delegate && [_delegate respondsToSelector:@selector(shareDimensionaViewSendTime:typeTime:)])
//        {
//            [_delegate shareDimensionaViewSendTime:beginTextField.text typeTime:NO];
//            [endTextField becomeFirstResponder];
//        }
        //此时如果开始时间与结束时间超过一个月则把结束时间修改成开始时间的后一天
        DDLOG(@"%@",endTimeLabel.text);
        if (endTimeLabel.text.length) {
            [self compareBegin:startTimeLabel.text to:endTimeLabel.text];
        }
        //设置最大时间，如果最大时间大于lastdate则就是lastdate
        [self setMaxDateforPickView:dateString];
        
    }
    else{
        NSDateFormatter *dateFomatter = [[NSDateFormatter alloc] init];
        [dateFomatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        [dateFomatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:localString]];
        
        NSComparisonResult result = [[dateFomatter dateFromString:startTimeLabel.text] compare:[dateFomatter dateFromString:[NSString stringWithFormat:@"%@ %@",dateString,timeString]]];
        if (result == NSOrderedAscending || result == NSOrderedSame) {
            endTimeLabel.text = [NSString stringWithFormat:@"%@ %@",dateString,timeString];
//            if(_delegate && [_delegate respondsToSelector:@selector(shareDimensionaViewSendTime:typeTime:)])
//            {
//                [_delegate shareDimensionaViewSendTime:endTextField.text typeTime:YES ];
//                
//            }
        }
        else
        {
            [PromptInfoView showMessage:NSLocalizedString(@"timeErrorChoose", nil)];
        }
    }
    timeSelectView.hidden = YES;
    backGroundView.hidden = YES;
    [self judgeShareBtnStatus];
}

- (void)judgeShareBtnStatus{
    if (mobilTextField.text.length!=0 && startTimeLabel.text.length!=0 && endTimeLabel.text.length!=0) {
        shareBtn.backgroundColor = UIColorFromRGB(0x7dc234);
        shareBtn.enabled = YES;
    }else{
        shareBtn.backgroundColor = UIColorFromRGB(0xc1c1c1);
        shareBtn.enabled = NO;
    }
}

#pragma mark - 手势
- (void)shareXieyi{
    UsingGuideController * vc = [[UsingGuideController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)cancelView{
    timeSelectView.hidden = YES;
    backGroundView.hidden = YES;
}

#pragma mark -----------  结束时间小于开始时间 结束时间大于开始时间的三十天
-(BOOL)compareBegin:(NSString *)begin to:(NSString *)end
{
    NSDateFormatter *dateFomatter = [[NSDateFormatter alloc] init];
    [dateFomatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    [dateFomatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:localString]];
    NSDate * beginDate = [dateFomatter dateFromString:begin];
    NSDate * newBeginDate = [NSDate dateWithTimeInterval:shareSecs sinceDate:beginDate];
    NSDate * endDate = [dateFomatter dateFromString:end];
    if ([self dateCompare:beginDate to:endDate]) {
        //开始时间大于等于结束时间
        endDate = [NSDate dateWithTimeInterval:24 * 3600 sinceDate:beginDate];
        if ([self dateCompare:endDate to:lastDate]) {
            //结束时间大于最终时间
            endDate = lastDate;
        }
    }
    else
    {
        //开始时间小于结束时间
        if (![self dateCompare:newBeginDate to:endDate]) {
            //结束时间比开始时间大于三十天
            endDate = [NSDate dateWithTimeInterval:24 * 3600 sinceDate:beginDate];
        }
    }
    endTimeLabel.text = [dateFomatter stringFromDate:endDate];
//    if(_delegate && [_delegate respondsToSelector:@selector(shareDimensionaViewSendTime:typeTime:)])
//    {
//        [_delegate shareDimensionaViewSendTime:endTextField.text typeTime:YES ];
//        
//    }
    
    return YES;
}

#pragma mark ---- 单纯的两个时间的比较 开始时间大于或者等于结束时间为yes  否则为no
-(BOOL)dateCompare:(NSDate *)beginDate to:(NSDate *)endDate
{
    NSComparisonResult result = [beginDate compare:endDate];
    if (result == NSOrderedDescending || result == NSOrderedSame) {
        return YES;
    }
    else
    {
        return NO;
    }
}

#pragma mark -------- 设置可以选择的最大日期
-(void)setMaxDateforPickView:(NSString *)string
{
    NSDateFormatter *dateFomatter = [[NSDateFormatter alloc] init];
    [dateFomatter setDateFormat:@"yyyy-MM-dd"];
    [dateFomatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:localString]];
    NSDate * beginDate = [dateFomatter dateFromString:string];
    NSDate * endDate = [NSDate dateWithTimeInterval:shareSecs sinceDate:beginDate];
    
    if ([self dateCompare:endDate to:lastDate]) {
        endDate = lastDate;
    }
    selectPickView.maximumDate = endDate;
    [selectPickView reloadInputViews];
}


#pragma mark - 选择地区
- (void)chooseArea{
    AreaChooseController * acc = [[AreaChooseController alloc]init];
    [self.navigationController pushViewController:acc animated:YES];
    acc.areaCode = ^(NSString * areaCodeString,NSString * areaString)
    {
        [areaBtn setTitle:[NSString stringWithFormat:@"+%@",areaCodeString] forState:UIControlStateNormal];
    };
}

#pragma mark - 从通讯录添加成员
- (void)chooseLinkman{
    ABPeoplePickerNavigationController * peoplePicker = [[ABPeoplePickerNavigationController alloc]init];
    peoplePicker.peoplePickerDelegate = self;
    [self presentViewController:peoplePicker animated:NO completion:nil];
}

#pragma mark -- ABPeoplePickerNavigationControllerDelegate
-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
    ABMutableMultiValueRef phoneMult = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSMutableArray *phones = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < ABMultiValueGetCount(phoneMult); i ++) {
        NSString * aphone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneMult, i);
        [phones addObject:aphone];
    }
    if (phones.count > 0) {
        mobilTextField.text = [phones objectAtIndex:0];
        mobilTextField.text = [mobilTextField.text stringByReplacingOccurrencesOfString:@"-" withString:@""];
    }
    
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
    [self judgeShareBtnStatus];
}

#pragma mark -- ABPeoplePickerNavigationControllerDelegate
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
}

//2_0到8_0系统使用的方法
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    ABMutableMultiValueRef phoneMult = ABRecordCopyValue(person, kABPersonPhoneProperty);
   
    NSMutableArray *phones = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < ABMultiValueGetCount(phoneMult); i ++) {
        NSString * aphone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneMult, i);
        [phones addObject:aphone];
    }
    if (phones.count > 0) {
        mobilTextField.text = [phones objectAtIndex:0];
        mobilTextField.text = [mobilTextField.text stringByReplacingOccurrencesOfString:@"-" withString:@""];
    }
    
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
    return NO;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
